package com.xipu.yygh.user.controller;


import com.xipu.yygh.common.result.R;
import com.xipu.yygh.common.utils.HeadJwtHelper;
import com.xipu.yygh.model.user.Patient;
import com.xipu.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author gql
 * @since 2022-05-10
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {
    /**
     * 就诊人表
     */
    @Autowired
    private PatientService patientService;

    /**
     * 获取就诊人列表
     * @param request 请求
     * @return r
     */
    @GetMapping("auth/findAll")
    public R findAll(HttpServletRequest request) {
        // 从请求头中获取token,再获取当前登录的用户id
        List<Patient> list = this.patientService.findAllPatient(HeadJwtHelper.getUserId(request));
        return R.ok().data("list", list);
    }

    /**
     * 添加就诊人
     * @param patient 就诊人
     * @param request 请求
     * @return r
     */
    @PostMapping("auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        // 从请求头中获取token,再获取当前登录的用户id
        patient.setUserId(HeadJwtHelper.getUserId(request));
        this.patientService.save(patient);
        return R.ok();
    }

    /**
     * 根据id获取就诊人信息
     * @param id id
     * @return r
     */
    @GetMapping("auth/get/{id}")
    public R getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return R.ok().data("patient", patient);
    }

    /**
     * 修改就诊人
     * @param patient 就诊人信息
     * @return r
     */
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return R.ok();
    }

    /**
     * 删除就诊人
     * @param id 就诊人id
     * @return r
     */
    @DeleteMapping("auth/remove/{id}")
    public R removePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return R.ok();
    }

    /**
     * 获取就诊人信息
     * @param id 就诊人Id
     * @return 就诊人信息
     */
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {
        return this.patientService.getById(id);
    }
}

